/**
 * @ignore
 * cookie
 * @author lifesinger@gmail.com
 */

var util = require('util');
var doc = document,
    MILLISECONDS_OF_DAY = 24 * 60 * 60 * 1000,
    encode = encodeURIComponent,
    decode = util.urlDecode;

function isNotEmptyString(val) {
    return (typeof val === 'string') && val !== '';
}

/**
 * Provide Cookie utilities.
 * @class KISSY.Cookie
 * @singleton
 */
module.exports = {

    /**
     * Returns the cookie value for given name
     * @return {String} name The name of the cookie to retrieve
     */
    get: function (name) {
        var ret, m;

        if (isNotEmptyString(name)) {
            if ((m = String(doc.cookie).match(
                new RegExp('(?:^| )' + name + '(?:(?:=([^;]*))|;|$)')))) {
                ret = m[1] ? decode(m[1]) : '';
            }
        }
        return ret;
    },

    /**
     * Set a cookie with a given name and value
     * @param {String} name The name of the cookie to set
     * @param {String} val The value to set for cookie
     * @param {Number|Date} expires
     * if Number specified how many days this cookie will expire
     * @param {String} domain set cookie's domain
     * @param {String} path set cookie's path
     * @param {Boolean} secure whether this cookie can only be sent to server on https
     */
    set: function (name, val, expires, domain, path, secure) {
        var text = String(encode(val)), date = expires;

        // 从当前时间开始，多少天后过期
        if (typeof date === 'number') {
            date = new Date();
            date.setTime(date.getTime() + expires * MILLISECONDS_OF_DAY);
        }
        // expiration date
        if (date instanceof Date) {
            text += '; expires=' + date.toUTCString();
        }

        // domain
        if (isNotEmptyString(domain)) {
            text += '; domain=' + domain;
        }

        // path
        if (isNotEmptyString(path)) {
            text += '; path=' + path;
        }

        // secure
        if (secure) {
            text += '; secure';
        }

        doc.cookie = name + '=' + text;
    },

    /**
     * Remove a cookie from the machine by setting its expiration date to sometime in the past
     * @param {String} name The name of the cookie to remove.
     * @param {String} domain The cookie's domain
     * @param {String} path The cookie's path
     * @param {String} secure The cookie's secure option
     */
    remove: function (name, domain, path, secure) {
        this.set(name, '', -1, domain, path, secure);
    }
};

/*
 2012.02.14 yiminghe@gmail.com
 - jsdoc added

 2010.04
 - get 方法要考虑 ie 下，
 值为空的 cookie 为 'test3; test3=3; test3tt=2; test1=t1test3; test3', 没有等于号。
 除了正则获取，还可以 split 字符串的方式来获取。
 - api 设计上，原本想借鉴 jQuery 的简明风格：S.cookie(name, ...), 但考虑到可扩展性，目前
 独立成静态工具类的方式更优。
 */
